package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.base.CurrentAccount;
import com.example.blog.entity.Favorites;
import com.example.blog.entity.FavoritesFolders;
import com.example.blog.entity.dto.favoritesFolders.FavoritesFoldersAddOrUpdateDTO;
import com.example.blog.entity.vo.favorites.FavoritesDetailInfoVO;
import com.example.blog.entity.vo.favoritesFolders.FavoritesFoldersVO;
import com.example.blog.mapper.FavoritesFoldersMapper;
import com.example.blog.service.FavoritesFoldersService;
import com.example.blog.service.FavoritesService;
import com.example.blog.util.BeanUtils;
import com.example.blog.util.CurrentUserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName FavoritesFoldersServiceImpl
 * @Description TODO
 * @Author Wang Li Hong
 * @date 2024/1/15 15:11
 * @Version 1.0
 */
@Service
public class FavoritesFoldersServiceImpl extends ServiceImpl<FavoritesFoldersMapper, FavoritesFolders> implements FavoritesFoldersService {


    @Autowired
    private FavoritesService favoritesService;
    @Override
    public List<FavoritesFoldersVO> dataList() {
        CurrentAccount user = CurrentUserUtils.currentUser();
        LambdaQueryWrapper<FavoritesFolders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FavoritesFolders::getCreatedBy, user.getId());
        List<FavoritesFolders> list = list(queryWrapper);
        List<Long> ffids = list.stream()
                .map(FavoritesFolders::getId)
                .toList();
        List<Favorites> favorites = favoritesService.getByFFId(ffids);
        Map<Long, Long> longMap = favorites.stream()
                .collect(Collectors.groupingBy(Favorites::getFavoritesFoldersId, Collectors.counting()));

        return list.stream()
                .map(favoritesFolders -> {
                    FavoritesFoldersVO detailInfoVO = BeanUtils.copyProperties(favoritesFolders, FavoritesFoldersVO.class);
                    detailInfoVO.setTotal(longMap.getOrDefault(favoritesFolders.getId(), 0L));
                    return detailInfoVO;
                }).toList();
    }


    @Override
    public void add(FavoritesFoldersAddOrUpdateDTO favoritesFoldersAddOrUpdateDTO) {
        FavoritesFolders favoritesFolders = BeanUtils.copyProperties(favoritesFoldersAddOrUpdateDTO, FavoritesFolders.class);

        saveOrUpdate(favoritesFolders);
    }

    @Override
    public List<FavoritesFoldersVO> publicList(Long userId) {
        LambdaQueryWrapper<FavoritesFolders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FavoritesFolders::getCreatedBy, userId);
        List<FavoritesFolders> list = list(queryWrapper);
        List<Long> ffids = list.stream()
                .map(FavoritesFolders::getId)
                .toList();
        List<Favorites> favorites = favoritesService.getByFFId(ffids);
        Map<Long, Long> longMap = favorites.stream()
                .collect(Collectors.groupingBy(Favorites::getFavoritesFoldersId, Collectors.counting()));
        return list.stream()
                .map(favoritesFolders -> {
                    FavoritesFoldersVO detailInfoVO = BeanUtils.copyProperties(favoritesFolders, FavoritesFoldersVO.class);
                    detailInfoVO.setTotal(longMap.getOrDefault(favoritesFolders.getId(), 0L));
                    return detailInfoVO;
                }).toList();
    }
}
